package cc.mrbird.febs.system.controller;

import cc.mrbird.febs.common.annotation.ControllerEndpoint;
import cc.mrbird.febs.common.utils.FebsUtil;
import cc.mrbird.febs.common.entity.FebsConstant;
import cc.mrbird.febs.common.controller.BaseController;
import cc.mrbird.febs.common.entity.FebsResponse;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.system.entity.Icon;
import cc.mrbird.febs.system.service.IIconService;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.wuwenze.poi.ExcelKit;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.*;
import java.util.HashMap;
import java.util.Enumeration;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;

/**
 *  Controller
 *
 * @author jeson
 * @date 2020-12-02 16:41:22
 */
@Slf4j
@Validated
@Controller
public class IconController extends BaseController {

    @Autowired
    private IIconService iconService;

    @GetMapping(FebsConstant.VIEW_PREFIX + "icon")
    public String iconIndex(){
        return FebsUtil.view("system/icon/icon");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX+"icon/iconList")
    public String iconList(Model model){
        List<Icon> iconList = iconService.list();
        model.addAttribute("icons",iconList);
        return FebsUtil.view("system/icon/iconList");
    }

    @GetMapping("icon")
    @ResponseBody
    @RequiresPermissions("icon:list")
    public FebsResponse getAllIcons(Icon icon) {
        return new FebsResponse().success().data(iconService.findIcons(icon));
    }

    @GetMapping("icon/list")
    @ResponseBody
    public FebsResponse iconList(QueryRequest request, HttpServletRequest req, Icon icon) {
        Enumeration enu=req.getParameterNames();
        HashMap<String,String> map=new HashMap<String,String>();
        while(enu.hasMoreElements()){
            String paraName=(String)enu.nextElement();
            map.put(paraName,req.getParameter(paraName));
        }
        Map<String, Object> dataTable = getDataTable(this.iconService.findIconDetail(request, map));
        return new FebsResponse().success().data(dataTable);
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "icon/add")
    public String goIconAdd() {
        return FebsUtil.view("system/icon/iconAdd");
    }

    @GetMapping(FebsConstant.VIEW_PREFIX + "icon/update/{id}")
    public String goIconUpdate(@PathVariable String id, Model model) {
        Icon obj= iconService.getById(id);
        model.addAttribute("obj",obj);
        return FebsUtil.view("system/icon/iconUpdate");
    }


    /**
     * 接收图标上传
     * @param file
     * @return
     */
    @RequestMapping("icon/iconUpload")
    @ResponseBody
    public FebsResponse iconUpload(MultipartFile file){
        byte[] bytes = new byte[0];
        try {
            bytes = file.getBytes();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new FebsResponse().success().data(bytes);
    }

    /**
     * 用于图标回显
     * @param id
     * @param res
     */
    @GetMapping("icon/iconShow")
    @ResponseBody
    public String showIcon(String id){
        Icon icon = iconService.getById(id);
        byte[] bytes = icon.getIconData();
        String r = "data:image/png;base64,"+new String(bytes);
        return r;

    }



    @ControllerEndpoint(operation = "新增Icon", exceptionMessage = "新增Icon失败")
    @PostMapping("icon")
    @ResponseBody
    public FebsResponse addIcon(@Valid Icon icon) {
        System.out.println("插入的："+icon.getIconData());
        this.iconService.createIcon(icon);
        return new FebsResponse().success();
    }

    @GetMapping("icon/delete/{objIds}")
    @ControllerEndpoint(operation = "删除", exceptionMessage = "删除失败")
    @ResponseBody
    public FebsResponse deleteIcon( @PathVariable String objIds) {
        String[] ids = objIds.split(StringPool.COMMA);
        this.iconService.deleteIcon(ids);
        return new FebsResponse().success();
    }

    @ControllerEndpoint(operation = "修改Icon", exceptionMessage = "修改Icon失败")
    @PostMapping("icon/update")
    @ResponseBody
    public FebsResponse updateIcon(Icon icon) {
        //判断传入的icon的iconData属性是否有值
        if(icon.getIconData().length==0 || icon.getIconData()==null){
            Icon icon1 = iconService.getById(icon.getId());
            icon.setIconData(icon1.getIconData());
        }
        this.iconService.updateIcon(icon);
        return new FebsResponse().success();
    }
}
